package demo.day1210;

import Common.DriverCommon;
import Util.CalculateDistanceByBboxUtil;
import Util.CalculateLocation;
import Util.Neo4jCalculatePointUtil;
import org.neo4j.driver.*;
import org.neo4j.driver.types.Node;

import java.util.List;

import static org.neo4j.driver.Values.parameters;

/**
 * 通过bbox给pitch数据加上near属性
 */
public class BboxDemo2 {

    public static void main(String[] args) {
        try (DriverCommon driverCommon = new DriverCommon()) {
            Driver driver = driverCommon.getGraphDatabase();
            Session session = driver.session();
            try (Transaction tx = session.beginTransaction()) {
                // 获取所有具有 'Pois' 标签的节点及其坐标信息
                Result result = tx.run("MATCH (n:Pois) WHERE n.bbox IS NOT NULL RETURN id(n) as nodeId, n.bbox as bbox,n ");
                while (result.hasNext()) {
                    Record record = result.next();
                    Node n1 = record.get("n").asNode();
                    String name1 = n1.get("name").asString();
                    long nodeId = record.get("nodeId").asLong();
                    List<Object> bbox = record.get("bbox").asList();

                    // 计算中心点或其他距离相关的操作
                    List<Object> centre = Neo4jCalculatePointUtil.calculateCenter(bbox);

                    // 获取其他具有 'Pois' 标签的节点的坐标信息
                    Result allNodesResult = tx.run("MATCH (n:Pois) WHERE id(n) <> $nodeId  AND n.bbox IS NOT NULL  RETURN id(n) as otherNodeId, n.bbox as otherBbox,n ",
                            parameters("nodeId", nodeId));

                    while (allNodesResult.hasNext()) {
                        Record otherRecord = allNodesResult.next();
                        long otherNodeId = otherRecord.get("otherNodeId").asLong();
                        List<Object> otherBbox = otherRecord.get("otherBbox").asList();
                        Node n2 = otherRecord.get("n").asNode();
                        String name2 = n2.get("name").asString();

                        // 计算中心点或其他距离相关的操作
                        List<Object> otherCentre = Neo4jCalculatePointUtil.calculateCenter(otherBbox);

                        // 计算距离
                        double distance = CalculateDistanceByBboxUtil.calculate(centre, otherCentre);
                        String location = CalculateLocation.calculateLocation(bbox,otherBbox);
                        String location2 = CalculateLocation.GetDirection(bbox,otherBbox);


//                        System.out.println(location);
                        // 如果距离小于200米，添加关系
                        if (distance < 300) {
                            System.out.println(name1+"位于"+location2+" "+location+"    "+name2+"距离: "+distance);
//                            tx.run("MATCH (n), (m) WHERE id(n) = $nodeId AND id(m) = $otherNodeId " +
////                                            " AND NOT EXISTS((n)-[:NEAR]-(m)) " +
////                                            " AND NOT EXISTS((m)-[:NEAR]-(n)) " +
//                                            " CREATE (n)-[r:NEAR {location: '"+ location +"'}]->(m) ",
//                                    parameters("nodeId", nodeId, "otherNodeId", otherNodeId));
                        }
                    }
                }
                // 提交事务
                tx.commit();
            }

        }

        }
    }
    

